package com.example.springboot.controller;

import com.example.springboot.Service.CourseSave;
import com.example.springboot.entity.Course;
import com.example.springboot.entity.Room;
import com.example.springboot.mapper.CourseMapper;
import com.example.springboot.mapper.Course_sortMapper;
import com.example.springboot.mapper.RoomMapper;
import com.example.springboot.mapper.TeacherMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

@RestController
@RequestMapping("course")
public class CourseController {
    @Autowired
    private CourseMapper mapper;

    @Autowired
    private CourseSave courseSave;

    @Autowired
    private TeacherMapper teacherMapper;


//    显示的数据
      public  Map<String,Object> showPade( List<Course> courses,String choose,Integer grade,Integer stuID){
        // 查教师
        courses.forEach(course -> {
            course.setTeacherName(teacherMapper.findTeacheName(course.getJob_id()));   // 教师id 查教师名
            course.setCourse_stuTime(courseSave.getTime(course.getCourse_id()));    //   课程id查课程时间
            course.setCourse_time(courseSave.getConflict(course.getCourse_id(),stuID));
                }
        );
        Map<String,Object> res = new HashMap<>();
        res.put("total",mapper.couraseTotal(grade,choose));   // 总条数
        res.put("data",courses);            // course +  教师名

        return res;
    }
    //分类查询

    //  课程分类  加  年级分类
    @GetMapping("/classPage")
    public   Map<String,Object>  classPage
    (@RequestParam Integer pageNum, @RequestParam Integer pageSize, @RequestParam Integer classes,@RequestParam Integer grade,@RequestParam Integer stuID)
    {
        pageNum = (pageNum - 1) * pageSize;

        //  类别
        //       1             2                      3
        //      11           21    22   23      31    32     33
        //    本年级 跨年级    人文  自然   艺术     球    田径  舞蹈武术

        String choose = choose(classes);
        if (choose == ""){
            Map<String,Object> res = new HashMap<>();
            res.put("massage" , "查询错误");
            return res;
        }
//        String choose = "专业选修课";
        List<Course> courses = mapper.courasPage_class(pageNum, pageSize, grade,choose);  // 查询的条件
        return showPade(courses,choose,grade,stuID);
    }
    public String choose(Integer classes){
        switch (classes){
            case 11: return "专业选修课";// 本年级
            case 12: return "专业选修课"; // 跨年级
            case 21: return "人文社科";//人文  自然   艺术
            case 22: return "自然科学";
            case 23: return "艺术素养";
            //球    田径  舞蹈武术
            case 31: return "体育球类";
            case 32: return "体育田径类";
            case 33: return "体育舞蹈武术类";
            default: return "";
        }

    }
    @GetMapping("TeacherCourse")
    public List<Course> TeacherCourse(@RequestParam Integer job_id){
          return mapper.TeacherCourse(job_id);
    }

//    课表
    @GetMapping("Schedule")
    public  HashMap<String,String> Schedule(@RequestParam Integer teacher_id,@RequestParam Integer student_id){
          List<Course> courses;
          if (teacher_id == 0){
              courses =   mapper.ScheduleStudent(student_id);
          }else {
              courses =   mapper.ScheduleTeacher(teacher_id);
          }
        return courseSave.schedle(courses);

    }
    @PostMapping("setCourseMsg")
    public Integer setCourseMsg(@RequestBody Course course){
          course.setJob_id(teacherMapper.getTeacher_id(course.getTeacherName()));
          return mapper.setCourseMsg(course);
    }
    @GetMapping("likeCourse")
    public List<Course> Like(@RequestParam String str,@RequestParam Integer stuID,@RequestParam Integer classes,@RequestParam Integer grade){
        Pattern pattern = Pattern.compile("[0-9]*");
          if (pattern.matcher(str).matches()){
//              是数字
            return mapper.Course_idSelect(str);
          }else {
              return mapper.courasPage_like("%"+str+"%");
          }
      }
}
